package engine.newUi
{
	import engine.control.NxTablePageTurner;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.UIManage;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	
	import newx.com.config.StaticIni;
	import newx.com.control.table.NxTabelPos;
	import newx.com.control.table.NxTable;
	import newx.com.control.table.TableCellFactory;
	import newx.com.control.window.NxWindowEvent;
	
	/**
	 *	 *跨服战 战报 
	 * @author yanghr
	 * 
	 */
	public class StrideWarfareLogic
	{
		private static var _self:StrideWarfareLogic;
		public static function getInstance():StrideWarfareLogic
		{
			if(_self==null)
			{
				_self=new StrideWarfareLogic();
			}
			return _self;
		}
		public function StrideWarfareLogic()
		{
		}
		private var _ui:StrideWarfareUI
		public function get ui():StrideWarfareUI 
		{
			if(!_ui)
			{
				_ui=new StrideWarfareUI;
				UIManage.getInstance().initUI(_ui,"StrideWarfareUI")
				_ui.Init();
			}
			return _ui;
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		
		private 	var 	_pageTurner:NxTablePageTurner;
		private var _tableCellFactory:TableCellFactory;
		
		private var _tableRecordArr:Vector.<BattleRecord>=new Vector.<BattleRecord>;
		public function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			
			ui;
			if(ui.victoryTable==null)return;
			var text:TextField=new TextField;
			_pageTurner=new NxTablePageTurner(_ui._pageUpBtn,_ui._pageNextBtn,_ui.victoryTable,_ui._pageNum);
			_ui.victoryTable.columWidthArr = [113,125,80,80];
			//加入到table中
			_tableCellFactory = new TableCellFactory(_ui.victoryTable,_ui. windowBG,
				text,3);
			_tableCellFactory.TablePageChangeData = tablePageChangeData; 
			
			//			SERVER_CUSTOMMSG_SIFT_RECORD
			//淘汰赛			
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SIFT_RECORD),openUI)
			//晋级赛	我的战报
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_RISE_MY_RACE),openMyRecord)
		}
		/**
		 //晋级赛 我的战绩
		 SERVER_CUSTOMMSG_RISE_MY_RACE = 200478
		 riseType (天榜地榜)
		 riseSite (战场序号)
		 target (敌对玩家)
		 winWheels (胜利轮数)
		 wheels (参与轮数)
		 siftStatus (上面发的那个)
		 * @param e
		 * 
		 */		
		private function openMyRecord(e:CustomMsgEvent):void
		{
			trace("StrideWarfareLogic.openMyRecord(e)",e.pList);
			_tableRecordArr	=	new Vector.<BattleRecord>;
			_ui.infoLab.visible=false;
			var arr:Array=e.pList;
			var isRe:int=arr.shift();
			//enum
			//			{
			//				MyRaceStatus_Fight	= 0,		//战斗中
			//					MyRaceStatus_Win	= 1,		//胜利
			//					MyRaceStatus_Sift	= 2,		//淘汰
			//			};
			if(isRe==2)
			{
				statelab=StaticIni.getSwfLangStr("Stride_9")//"你已经被淘汰";
			}
			else if(isRe==0)
			{
				statelab=""//"当前第"+turns+"轮，剩余玩家数 ："+leftPlayers;
			}
			else if(isRe==1)
			{
				statelab=StaticIni.getSwfLangStr("Stride_8")//"你已经被晋级";
			}
			var len:int=arr.length;
			for (var i:int = 0; i < len; i+=6) 
			{
				var type:int=arr.shift();
				var index:int=arr.shift();
				var target:String=arr.shift();
				var winWheels:int=arr.shift();
				var wheels:int=arr.shift();
				//				var siftStatus:int=arr.shift();
				adddMultRecord(type,index,target,winWheels,wheels)
			}
			refreshRecord();
			if(!_ui.created)
			{
				_ui.showOrHide();
			}
			
		}
		private function openUI(e:CustomMsgEvent):void
		{
			trace("StrideWarfareLogic.openUI(e)",e.pList);
			_tableRecordArr	=	new Vector.<BattleRecord>;
			_ui.infoLab.visible=true;
			state=e.pList[0];
			var isRe:int=e.pList[2]
			var turns:int=e.pList[1];
			var leftPlayers:int=e.pList[3];
			if(isRe==0)
			{
				statelab=StaticIni.getSwfLangStr("Stride_9")//"你已经被淘汰";
			}
			else if(isRe==1)
			{
				statelab=StaticIni.getSwfLangStrVar(getStateString(),[turns,leftPlayers])//"当前第"+turns+"轮，剩余玩家数 ："+leftPlayers;
			}
			else if(isRe==2)
			{
				statelab=StaticIni.getSwfLangStr("Stride_8")//"你已经被晋级";
			}
			var len:int=(e.pList.length-4); 
			//			e.pList[4];
			for (var i:int = 0; i < len	; i+=4) 
			{
				addRecord(e.pList[4+i],e.pList[4+i+1],e.pList[4+i+2],e.pList[4+i+3])
			}
			refreshRecord();
			if(!_ui.created)
			{
				_ui.showOrHide();
			}
			
		}
		private function tablePageChangeData():void
		{
			var _tab:NxTable=_ui.victoryTable;
			var pageCount:int = _tab.pageLinesConut;
			for(var i:int=0; i<pageCount; ++i)
			{
				var pn:String = _tab.getItemFrom(
					new NxTabelPos(i+_tab.currentPage*pageCount));
				
				var cell:TextField;
				trace("StrideWarfareLogic.tablePageChangeData()",pn);
				if(pn != "" && pn != null)				
				{
					cell = _tableCellFactory.ArrTabelCell[i]
					if(cell == null)
						continue;
					cell.htmlText=_tableRecordArr.length>i?_tableRecordArr[i].toLinkString():""//getOperationStr(pn);
					cell.addEventListener(TextEvent.LINK, linkEventHandle);					
				}
			}
		}
		private function linkEventHandle(e:TextEvent):void
		{
			var link:String=e.text;
			if(link)
			{
				trace("StrideWarfareLogic.linkEventHandle(e)",link);
				//				SceneTransfer.getInstance().tranferTo(link);
				var arr:Array=link.split("!!");
				arr=arr.map(function (item:String,index:int,arr:Array):int{
					return int(item)
				})
				if(arr.length>1)
				{
					StrideMainLogic.getInstance().ui.hide();
					SystemSender.getInstance().CustomMsgEx(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_RISE_REPORT,arr);
				}
			}
		}
		public function	BeforeShow():void
		{
			_ui.victoryTable.columWidthArr = [113,125,80,80];
			_ui.victoryTable.dragLineLock=[1,2,3];	
			addAllEventListener()
			//			addRecord([]);
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
		}
		public function addAllEventListener():void
		{
			_pageTurner.addEventListener();
			//			_ui.closeBtn.addEventListener(MouseEvent.CLICK,close);
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
		}
		public function removeAllEventListener():void
		{
			_pageTurner.removeEventListener();
			//			_ui.closeBtn.removeEventListener(MouseEvent.CLICK,close)
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close);
		}
		/**
		 *
		 * 	第一列: 第几轮
		 第二列: 对手
		 第三列: 比分
		 第四列: 胜负 
		 * @param arr
		 * 
		 */		
		private function addRecord(type:int,turns:int,fighter:String,result:int):void
		{
			var record:BattleRecord=new BattleRecord();
			record.fighttype=	type;
			record.result	=	result;
			record.type		=	1
			record.fighter	=	fighter//"小7";
			record.times	=	turns;
			_tableRecordArr.push(record);
			
		}
		private function adddMultRecord(type:int,turns:int,fighter:String,winwheel:int,wheel:int):void
		{
			var record:BattleRecord=new BattleRecord();
			record.fighttype=	type;
			record.winWheel	=	winwheel;
			record.wheeltotal	=	wheel;
			record.type		=	2;
			record.fighter	=	fighter//"小7";
			record.times	=	turns;
			_tableRecordArr.push(record);
		}
		private function refreshRecord():void
		{
			var len:int=_tableRecordArr.length;
			_ui.victoryTable.dataProvider=null;
			_tableRecordArr=_tableRecordArr.reverse();
			_tableCellFactory.destroy();
			for (var i:int = 0; i < len; i++) 
			{
				var record:BattleRecord=_tableRecordArr[i]
				_ui.victoryTable.addLineByIndex([record.timeStr,(record.fighter=="")?"轮空":record.fighter,record.score,""],-1);
			}
			_pageTurner.onPageDis();
			_tableCellFactory.tableTurn();
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
		private function set statelab(str:String):void
		{
			_ui.stateLab.htmlText=str;
		}
		private var _state:int;
		/**
		 *3.天榜淘汰赛; 4.地榜淘汰赛; 6.天榜淘汰赛结束休息; 7.地榜淘汰赛结束休息; 
		 * @return 
		 * 
		 */		
		public function get state():int 
		{ 
			return _state; 
		}
		
		public function set state(value:int):void
		{
			if (_state == value)
				return;
			_state = value;
		}
		private function getStateString():String
		{
			if(_state==3||_state==6)
				return "Stride_6"
			return "Stride_7"
		}
		
	}
}
import engine.newUi.StrideMainLogic;
import engine.tip.TipsMainLogic;

import newx.com.config.StaticIni;

class BattleRecord
{
	/**
	 * 0,
	 * 1,白
	 * 2.绿
	 * 3.蓝
	 * 4.紫红
	 * 5.橙黄
	 * 6.黄
	 * 7.浅黄
	 * 8.红色
	 */		
	protected const COLOR_TABLE	:Array	=	["","#ffffff'>","#1eff00'>","#20BFF8'>","#a335ee'>","#E8912E'>","#e5cc80'>","#ffff00'>","#ff0000'>"];
	/**
	 *两种比赛结果类型 
	 */	
	public static const TYPE_SINGLE:int=1,TYPE_MULT:int=2;
	public var type:int;
	/**
	 *比赛类型 
	 * 		本地比赛  天榜淘汰赛 FightStatus_LOCALTOPSIFT  地榜淘汰赛 FightStatus_LOCALBOTTOMSIFT 天榜晋级赛 FightStatus_LOCALRISE_TOP 地榜晋级赛 FightStatus_LOCALRISE_BOTTOM
	 * 		跨副比赛  跨服天榜淘汰赛 FightStatus_CROSSTOPSIFT 跨服地榜淘汰赛 FightStatus_CROSSBOTTOMSIFT  夸服天榜晋级赛 FightStatus_CROSSRISE_TOP 夸服地榜晋级赛 FightStatus_CROSSRISE_BOTTOM
	 * 
	 */	
	private static const FightStatus_LOCALTOPSIFT:int=3,FightStatus_LOCALBOTTOMSIFT:int=4,FightStatus_LOCALRISE_TOP:int=8,FightStatus_LOCALRISE_BOTTOM:int=9,
		FightStatus_CROSSTOPSIFT:int=10,FightStatus_CROSSBOTTOMSIFT:int=11,FightStatus_CROSSRISE_TOP:int=15,FightStatus_CROSSRISE_BOTTOM:int=16;
	
	private const RESULT_TRAS:Array=[link_color("负",8),link_color("胜",1)];
	public var result:int;
	
	//	public var multResult:Array;
	public var winWheel:int
	
	public var wheeltotal:int
	
	/**
	 *类型（天地） 
	 */	
	public var fighttype:int
	/**
	 *当前场次 
	 */	
	public var times:int
	/**
	 *对手 
	 */	
	public var fighter:String
	protected function link_color(str:String,color:int):String
	{
		if(color < 1 || color >8)
		{
			color	=	1;
		}
		return ("<font color ='"+COLOR_TABLE[color]+str+"</font>");
	}
	
	public function toLinkString():String
	{
		var _tip:TipsMainLogic=TipsMainLogic.instance;
		if(type==TYPE_SINGLE)
			return getBaseResult();//RESULT_TRAS[result]//_tip.getUnderLineHtml(RESULT_TRAS[result],link);
		return getMultResult();
	}
	private function getBaseResult():String
	{
		return StaticIni.getSwfLangStr(result==1?"UIString_1000596":"UIString_1000595");
	}
	public function get score():String
	{
		if(type==TYPE_SINGLE)
		{
			return StaticIni.getSwfLangStrVar("Stride_5",(result==1?[1,0]:[0,1]));
		}
		//		Stride_5
		//		{数字}:{数字}
		//		StaticIni.getSwfLangStrVar("Stride_5",[winWheel,wheeltotal-winWheel]);
		if(wheeltotal<3) return "";
		return StaticIni.getSwfLangStrVar("Stride_5",[winWheel,wheeltotal-winWheel]);
		
	}
	private function getMultResult():String
	{
		var _tip:TipsMainLogic=TipsMainLogic.instance;
		var stri:String="";
		if(wheeltotal<3) return "";
		for (var i:int = 0; i < wheeltotal; i++) 
		{
			stri+=	_tip.getUnderLineHtml(getTurnString(i),fighttype+"!!"+times+"!!"+(i+1))+"   ";
		}
		return stri
		function getTurnString(index:int):String
		{
			return (i+1)+""
		}
	}
	public function get timeStr():String
	{
		var str:String="";
		var num:int;
		switch(fighttype)
		{
			//天帮淘汰
			case FightStatus_LOCALTOPSIFT: 	
			case FightStatus_CROSSTOPSIFT:
				str ="Stride_1";
				num =times;
				break;
			//地磅淘汰
			case FightStatus_LOCALBOTTOMSIFT:
			case FightStatus_CROSSBOTTOMSIFT:
				str ="Stride_2";
				num =times;
				break;
			//天帮晋级
			case FightStatus_LOCALRISE_TOP:
			case FightStatus_CROSSRISE_TOP:
				num=getIndexFromTurn(times);
				str =str ="StrideRound_"+(num);;	
				break;
			//地磅晋级
			case FightStatus_LOCALRISE_BOTTOM:
			case FightStatus_CROSSRISE_BOTTOM:
				num=getIndexFromTurn(times);
				str ="StrideRound_"+(num+5);
				break;
		}
		function getIndexFromTurn(index:int):int
		{
			var turn:int;
			if(index>=1 &&index<=16)
			{
				turn=1;
			}
			else if(index>=17 && index<=24)
			{
				turn=2;
			}
			else if(index >=25 && index<=28)
			{
				turn =3;
			}
			else if(index>=29 && index<=30)
			{
				turn =4;
			}
			else if(index== 31)
			{
				turn =5;
			}
			return turn;
		}
		//		var str:String=(fighttype==FightStatus_LOCALTOPSIFT||fighttype==FightStatus_LOCALRISE_TOP||fighttype==FightStatus_CROSSTOPSIFT||fighttype==FightStatus_CROSSRISE_TOP)?"Stride_1":"Stride_2";
		return StaticIni.getSwfLangStrVar(str,[num]);
	}
}